home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / answrbok / 6_1.lha / 6_1 / strclass.h < prev    next >
C/C++ Source or Header  |  1993-08-08  |  2KB  |  137 lines

  1. * Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
  2. * The C++ Answer Book */
  3. * Tony Hansen */
  4. * All rights reserved. */
  5. include <stream.h>
  6. include <string.h>
  7.  
  8. lass string {
  9.    struct srep {
  10. char *s;
  11. int n;
  12.    };
  13.    srep *p;
  14.  
  15. ublic:
  16.    string(char *);    // string x = "abc"
  17.    string();        // string x;
  18.    string(string&);    // string x = string ...
  19.    string& operator=(char *);
  20.    string& operator=(string &);
  21.    ~string();
  22.    char& operator[](int i);
  23. ifdef _6_3        // DELETE
  24.  include <6_3st1.h>    // DELETE
  25. endif /* _6_3 */    // DELETE
  26.  
  27.    friend ostream& operator<<(ostream&, string&);
  28.    friend istream& operator>>(istream&, string&);
  29.  
  30.    friend int operator==(string &x, char *s)
  31. { return strcmp(x.p->s, s) == 0; }
  32.  
  33.    friend int operator==(string &x, string &y)
  34. { return strcmp(x.p->s, y.p->s) == 0; }
  35.  
  36.    friend int operator!=(string &x, char *s)
  37. { return strcmp(x.p->s, s) != 0; }
  38.  
  39.    friend int operator!=(string &x, string &y)
  40. { return strcmp(x.p->s, y.p->s) != 0; }
  41.  
  42. ifdef _6_1            // DELETE
  43. riend class string_iterator;    // DELETE
  44.  ifdef A            // DELETE
  45.   include <6_1a1a.c>        // DELETE
  46.  endif                // DELETE
  47.  ifdef B            // DELETE
  48.   include <6_1a2a.c>        // DELETE
  49.  endif                // DELETE
  50.             // DELETE
  51.    friend string operator+(string& s1, string& s2);        // DELETE
  52.    string& operator+=(string& s2);                // DELETE
  53. endif /* _6_1 */    // DELETE
  54.  
  55. ifdef _6_2        // DELETE
  56.    string operator()(int i, int len = -1);            // DELETE
  57. endif /* _6_2 */    // DELETE
  58.  
  59. ;
  60.  
  61. tring::string()
  62.  
  63.    p = new srep;
  64.    p->s = 0;
  65.    p->n = 1;
  66.  
  67.  
  68. tring::string(char *s)
  69.  
  70.    p = new srep;
  71.    p->s = new char[ strlen(s) + 1 ];
  72.    strcpy(p->s, s);
  73.    p->n = 1;
  74.  
  75.  
  76. tring::string(string& x)
  77.  
  78.    x.p->n++;
  79.    p = x.p;
  80.  
  81.  
  82. tring::~string()
  83.  
  84.    if (--p->n == 0) {
  85. delete p->s;
  86. delete p;
  87.    }
  88.  
  89.  
  90. tring& string::operator=(char *s)
  91.  
  92.    if (p->n > 1) {    // disconnect self
  93. p->n--;
  94. p = new srep;
  95.    } else if (p->n == 1)
  96. delete p->s;
  97.  
  98.    p->s = new char[ strlen(s) +1 ];
  99.    strcpy(p->s, s);
  100.    p->n = 1;
  101.    return *this;
  102.  
  103.  
  104. tring& string::operator=(string& x)
  105.  
  106.    x.p->n++;
  107.    if (--p->n == 0) {
  108. delete p->s;
  109. delete p;
  110.    }
  111.    p = x.p;
  112.    return *this;
  113.  
  114.  
  115. stream& operator<<(ostream& s, string& x)
  116.  
  117.    return s << x.p->s << " [" << x.p->n << "]\n";
  118.  
  119.  
  120. stream& operator>>(istream& s, string& x)
  121.  
  122.    char buf[256];
  123.    s >> buf;
  124.    x = buf;
  125.    cout << "echo: " << x << "\n";
  126.    return s;
  127.  
  128.  
  129. include <error.h>
  130.  
  131. har& string::operator[] (int i)
  132.  
  133.    if (i < 0 || strlen(p->s) < i)
  134. error("index out of range");
  135.    return p->s[i];
  136.  
  137.